package com.xyh.transaction.meilisearch;

import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.TaskInfo;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class demo01 {

    public static void main(String[] args) throws MeilisearchException {
        JSONObject j1 = new JSONObject()
                .put("id", "1")
                .put("title", "Carol")
                .put("genre", new JSONArray("[\"Romance\",\"Drama\"]"));

        JSONObject j2 = new JSONObject()
                .put("id", "2")
                .put("title", "Wonder Woman")
                .put("genres", new JSONArray("[\"Action\",\"Adventure\"]"));

        JSONObject j3 = new JSONObject()
                .put("id", "3")
                .put("title", "Life of Pi")
                .put("genres", new JSONArray("[\"Adventure\",\"Drama\"]"));

        JSONObject j4 = new JSONObject()
                .put("id", "4")
                .put("title", "Mad Max: Fury Road")
                .put("genres", new JSONArray("[\"Adventure\",\"Science Fiction\"]"));

        JSONObject j5 = new JSONObject()
                .put("id", "5")
                .put("title", "Moana")
                .put("genres", new JSONArray("[\"Fantasy\",\"Action\"]"));

        JSONObject j6 = new JSONObject()
                .put("id", "6")
                .put("title", "Philadelphia")
                .put("genres", new JSONArray("[\"Drama\"]"));



        List<JSONObject> jsonObjects = new ArrayList<>();
        jsonObjects.add(j1);
        jsonObjects.add(j2);
        jsonObjects.add(j3);
        jsonObjects.add(j4);
        jsonObjects.add(j5);
        jsonObjects.add(j6);
        Client client = new Client(new Config("http://localhost:7700", "aSampleMasterKey"));

// An index is where the documents are stored.
        Index index = client.index("movies");

// If the index 'movies' does not exist, MeiliSearch creates it when you first add the documents.
        for (JSONObject jsonObject : jsonObjects) {
            String documents = jsonObject.toString();
            TaskInfo taskInfo = index.addDocuments(documents,"id");
            System.out.println(taskInfo);
        }
    }

}
